home *** CD-ROM | disk | FTP | other *** search
/ Web Designer 98 (Professional) / WebDesigner 1.0.iso / tutorials / tutorial / name2.txt < prev    next >
Encoding:
Text File  |  1997-06-15  |  2.1 KB  |  73 lines

  1. <script language="javascript">
  2.  
  3. <!--
  4.  
  5. var username = GetCookie('username');
  6. if (username == null) {
  7.   username = prompt('Please enter your name (otherwise press cancel)',"");
  8.   if (username == null) {
  9.     alert('Its ok if you dont want to tell me your name');
  10.     username = 'WebSurfer';
  11.  
  12.   } else {
  13.  
  14.     // don't set the username cookie.... because the user refused to tell you.
  15.     // they will be prompted next time....
  16.     pathname = location.pathname;
  17.     myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
  18.     // set expiry date to 1 year from now.
  19.     var largeExpDate = new Date ();
  20.     largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
  21.     SetCookie('username',username,largeExpDate,myDomain);
  22.  
  23.   }
  24.  
  25. }
  26.  
  27. function getCookieVal (offset) {
  28.    var endstr = document.cookie.indexOf (";", offset);
  29.    if (endstr == -1)
  30.       endstr = document.cookie.length;
  31.    return unescape(document.cookie.substring(offset, endstr));
  32.  
  33. }
  34.  
  35. function GetCookie (name) {
  36.         var arg = name + "=";
  37.         var alen = arg.length;
  38.         var clen = document.cookie.length;
  39.         var i = 0;
  40.         while (i < clen) {
  41.                 var j = i + alen;
  42.                if (document.cookie.substring(i, j) == arg)
  43.                        return getCookieVal (j);
  44.                i = document.cookie.indexOf(" ", i) + 1;
  45.                         if (i == 0)
  46.                                 break;
  47.                 }
  48.  
  49.    return null;
  50. }
  51.  
  52. function SetCookie (name, value) {
  53.         var argv = SetCookie.arguments;
  54.         var argc = SetCookie.arguments.length;
  55.         var expires = (argc > 2) ? argv[2] : null;
  56.         var path = (argc > 3) ? argv[3] : null;
  57.         var domain = (argc > 4) ? argv[4] : null;
  58.         var secure = (argc > 5) ? argv[5] : false;
  59.         document.cookie = name + "=" + escape (value) +
  60.                 ((expires == null) ? "" : ("; expires=" +
  61. expires.toGMTString())) +
  62.                 ((path == null) ? "" : ("; path=" + path)) +
  63.                 ((domain == null) ? "" : ("; domain=" + domain)) +
  64.                 ((secure == true) ? "; secure" : "");
  65.  
  66. }
  67.  
  68. document.write('<p>Thanks for visiting, '+username);
  69.  
  70. //-->
  71.  
  72. </script>
  73.